package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.util.CheckSignatureUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.rmi.server.ExportException;

/**
 * index控制器
 */
@Controller
public class IndexController {

  /**
   * 主页页面
   * @return API response html
   */
//  @GetMapping
//  public String index() {
//
//    return "index";
//  }

  @GetMapping("/")
  public String checkToken(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String signature = req.getParameter("signature");
    String timestamp = req.getParameter("timestamp");
    String nonce = req.getParameter("nonce");
    String echostr = req.getParameter("echostr");
    PrintWriter out = resp.getWriter();
    //调用比对signature的方法，实现对token和传入的参数进行hash算法后的结果比对
    System.out.println("" + signature + "--" + timestamp + "--" + nonce + "--" + echostr);
    if (CheckSignatureUtil.checkSignature(signature, timestamp, nonce)) {
      out.print(echostr);
    }
    return echostr;
  }

}
